/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cassandrawindow;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 *
 * @author MT844
 */
public class Promotions {
    public static final float PROMOTIONS = (float) 0.15;
    public void handle(){
        
        StringBuilder sb = new StringBuilder();
        
        MysqlDataManager67 mysql67 = new MysqlDataManager67();
        Map<String, String> MapData = new HashMap<>();
        String currentDate = HandleDataTableDomainDate.handleDateForPromotions();
        
        //String currentDate = "2013-05-07";        
        MapData = mysql67.getDataForPromotion(currentDate);
        
        Iterator iterator = MapData.entrySet().iterator();                   
        while (iterator.hasNext()) {
            Map.Entry mapEntry = (Map.Entry) iterator.next();        
            String userid = (String) mapEntry.getKey();
            String value = (String) mapEntry.getValue();
            String[] arrValue = new String[10];
            arrValue  = value.split(",");
            String username     =   arrValue[0];            
            int money           =   Integer.valueOf(arrValue[1]);
            int maxValueDay     =   Integer.valueOf(arrValue[2]);
            int balance         =   Integer.valueOf(arrValue[3]);
            int promotions      =   Integer.valueOf(arrValue[4]);
            int overdraft       =   Integer.valueOf(arrValue[5]);
            
            int promotionMoney = 0;
            int moneyCharge = 0;
            int moneySub  = 0;
            int mn_promotion =0;
            int mn_balance =0 ;      
         
            if(balance >= money){
                if(money >= maxValueDay)
                {                    
                    promotionMoney = (int) (PROMOTIONS*maxValueDay);                    
                    moneyCharge = maxValueDay;
                    moneySub = money - maxValueDay;
                }                               
                else {                                        
                    promotionMoney = (int) (PROMOTIONS*money);
                    moneyCharge = money;
                    moneySub  = 0;
                }
                mn_balance = money;
                mn_promotion = 0;
                
            } else if((balance < money)) {
                if(balance > maxValueDay)
                {                    
                    promotionMoney  = (int) (PROMOTIONS*maxValueDay);                    
                    moneyCharge     = maxValueDay;
                    moneySub        = money - maxValueDay;                    
                } else if((money >= maxValueDay)&&(maxValueDay >= balance)) {                                        
                    promotionMoney  = (int) (PROMOTIONS*balance);
                    moneyCharge     = money;
                    moneySub        = 0;
                } else if((maxValueDay >= money)) {                                        
                    promotionMoney  = (int) (PROMOTIONS*balance);
                    moneyCharge     = money;
                    moneySub        = 0;
                }
                mn_balance = balance;
                mn_promotion = money - balance;                
            }
            
            String str = "(" + userid + "," + username + ","+ money + "," + mn_balance + "," + mn_promotion  + "," + promotionMoney + ",'" + moneySub + "," + moneyCharge+ ",'" + balance + ",'" + promotions + "," + overdraft + "," + maxValueDay+ ",'" + currentDate + "')";
            sb.append(str).append(",");            
        }
        
        //xu li cho du lieu cuoi
        if(sb.toString().length() > 1)
        {
            String relStr = sb.toString();
            String valInsert = relStr.substring(0, (relStr.length()-1));        
            HandleImportData hid = new HandleImportData();
            String pathFile="";        
            try {
                pathFile = hid.outToCsvPromotions(valInsert);
                Debug.println("pathFile:"+pathFile);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            try {                                           
                MysqlDataManager67 mysql = new MysqlDataManager67(); 
                mysql.deletePromotion(currentDate);
                mysql.loadToV3PromotionsFromFile(pathFile);
                mysql.shutdownConnection();                        
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        mysql67.shutdownConnection();
        
    }                                                            
}
